package com.cc.Controller;
import com.cc.entity.EOrder;
import com.cc.Service.IEOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/order")
public class EOrderController {
    @Autowired
    private IEOrderService iEOrderService;
    @RequestMapping("/List")
  /* public String list(Model model)S{
        List<EbOrder> ebOrders=iEbOrderService.listByPage(1,5);
        model.addAttribute("ebOrders",ebOrders);
        model.addAttribute("curPage",);
        return "manage/order";
*/

   public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") int page,@RequestParam(value = "pageSize",defaultValue = "3")int pageSize){
        List<EOrder> eOrders=iEOrderService.listByPage(page,pageSize);
        System.out.println(eOrders);
        ModelAndView mv=new ModelAndView();
        mv.addObject("eOrders",eOrders);
        mv.addObject("curPage",page);

        int totalNum=iEOrderService.total();//获取记录条数
        double no=(double) totalNum/pageSize;//获取页数
        int num=(int)Math.ceil(no);//向上取整
        mv.addObject("totalPage",num);
        mv.setViewName("manage/order");
        return mv;
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id){
       int result = iEOrderService.delete(id);
       if(result<=0){
           //统一处理异常
           return "manage/error";
       }
         return "manage/manage-result";

    }
    //修改页面
    @RequestMapping("/edit/{eoId}")
    public ModelAndView edit(@PathVariable("eoId") int eoId){
        EOrder eOrder=iEOrderService.selectByCondition(eoId);
        ModelAndView mv=new ModelAndView();
        mv.addObject("ebOrder",eOrder);
        mv.setViewName("manage/order-modify");
        return mv;
   }
   //修改处理
   @RequestMapping("/editSubmit")
   public String editSubmit(EOrder eOrder){
           int rusult = iEOrderService.updateById(eOrder);
            if (rusult <= 0) {
               return "manage/error";
            }
           return "manage/manage-result";

   }
    @RequestMapping(value="/select",method = RequestMethod.POST)
    public ModelAndView select(@RequestParam(value = "eoId") Integer eoId,@RequestParam(value = "eoUserName") String eoUserName){
       EOrder order=new EOrder();
       int num;
       if(eoId==null){
           num=0;
       }else{
           num=eoId;
       }
        order.setEoId(num);
        order.setEoUserName(eoUserName);
        List Order=iEOrderService.select(order);
        ModelAndView mv=new ModelAndView();
        mv.addObject("eOrder",Order);
        mv.setViewName("manage/order-select");
        return mv;
    }




}
